package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbSeckillGoodsExample;
import com.pyg.pojo.TbSeckillGoodsExample.Criteria;
import com.pyg.mapper.TbSeckillGoodsMapper;
import com.pyg.pojo.TbSeckillGoods;
import com.pyg.goods.service.TbSeckillGoodsService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbSeckillGoodsServiceImpl implements TbSeckillGoodsService {
	@Autowired
	private TbSeckillGoodsMapper tbSeckillGoodsMapper;

	@Override
	public List<TbSeckillGoods> findAll() {
		return tbSeckillGoodsMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbSeckillGoods> page = (Page<TbSeckillGoods>) tbSeckillGoodsMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbSeckillGoods
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbSeckillGoods tbSeckillGoods, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbSeckillGoodsExample example = null;
		if (tbSeckillGoods != null) {
			example = new TbSeckillGoodsExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getGoodsId())) {
				criteria.andGoodsIdEqualTo(tbSeckillGoods.getGoodsId() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getItemId())) {
				criteria.andItemIdEqualTo(tbSeckillGoods.getItemId() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getTitle())) {
				criteria.andTitleLike("%" + tbSeckillGoods.getTitle() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getSmallPic())) {
				criteria.andSmallPicLike("%" + tbSeckillGoods.getSmallPic() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getPrice())) {
				criteria.andPriceEqualTo(tbSeckillGoods.getPrice() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getCostPrice())) {
				criteria.andCostPriceEqualTo(tbSeckillGoods.getCostPrice() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getSellerId())) {
				criteria.andSellerIdLike("%" + tbSeckillGoods.getSellerId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getCreateTime())) {
				criteria.andCreateTimeEqualTo(tbSeckillGoods.getCreateTime() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getCheckTime())) {
				criteria.andCheckTimeEqualTo(tbSeckillGoods.getCheckTime() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getStatus())) {
				criteria.andStatusLike("%" + tbSeckillGoods.getStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getStartTime())) {
				criteria.andStartTimeEqualTo(tbSeckillGoods.getStartTime() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getEndTime())) {
				criteria.andEndTimeEqualTo(tbSeckillGoods.getEndTime() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getNum())) {
				criteria.andNumEqualTo(tbSeckillGoods.getNum() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getStockCount())) {
				criteria.andStockCountEqualTo(tbSeckillGoods.getStockCount() );
			}
			if (PygStringUtils.isNotEmpty(tbSeckillGoods.getIntroduction())) {
				criteria.andIntroductionLike("%" + tbSeckillGoods.getIntroduction() + "%");
			}
		}

		Page<TbSeckillGoods> page = (Page<TbSeckillGoods>) tbSeckillGoodsMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbSeckillGoods tbSeckillGoods) {
		tbSeckillGoodsMapper.insert(tbSeckillGoods);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbSeckillGoodsMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbSeckillGoods findOne(Long id) {
		return tbSeckillGoodsMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbSeckillGoods tbSeckillGoods) {
		tbSeckillGoodsMapper.updateByPrimaryKey(tbSeckillGoods);
	}

}
